
import { _decorator, Component, Node, JsonAsset } from 'cc';
import { GlobalEnum } from '../Script/GameSpecial/GlobalEnum';
const { ccclass, property } = _decorator;

@ccclass('EditorCollerItem')
export class EditorCollerItem extends Component {

    //#region 玩家位置
    @property(Node)
    protected player: Node = null;
    protected getPlayerPos() {
        let p = this.player.getWorldPosition();
        return { x: parseFloat(p.x.toFixed(2)), y: 0, z: parseFloat(p.z.toFixed(2)) };
    }
    //#endregion

    //#region 传送门位置
    @property(Node)
    protected portalLayer: Node = null;
    protected getPortalPos() {
        let points = [];
        let children = this.portalLayer.children;
        for (let i = 0, c = children.length; i < c; ++i) {
            let node = children[i];
            let p = node.getWorldPosition();
            points.push({ x: parseFloat(p.x.toFixed(2)), y: 0, z: parseFloat(p.z.toFixed(2)) });
        }
        return points;
    }
    //#endregion

    //#region 碰撞体
    @property(Node)
    protected colliderLayer: Node = null;
    protected getColliderData(node: Node) {
        let children = node.children;
        let points = [];
        for (let i = 0, c = children.length; i < c; ++i) {
            let node = children[i];
            let p = node.getWorldPosition();
            points.push(parseFloat(p.x.toFixed(2)), parseFloat((-p.z).toFixed(2)));
        }
        return points;
    }
    protected getCollider() {
        let colliders = [];
        let children = this.colliderLayer.children;
        for (let i = 0, c = children.length; i < c; ++i) {
            let node = children[i].getChildByName("1");
            if (!!node) {
                colliders.push(this.getColliderData(node));
            }
        }
        return colliders;
    }
    //#endregion

    //#region 敌人出现位置
    @property(Node)
    protected enemyLayer: Node = null;
    protected getEnemiesPos() {
        let points = [];
        let children = this.enemyLayer.children;
        for (let i = 0, c = children.length; i < c; ++i) {
            let node = children[i];
            let p = node.getWorldPosition();
            points.push({ x: parseFloat(p.x.toFixed(2)), y: 0, z: parseFloat(p.z.toFixed(2)) });
        }
        return points;
    }
    //#endregion

    //#region 静态障碍物
    @property(Node)
    protected staticBarrier: Node = null;
    protected getStaticBarriers() {
        let points = [];
        let children = this.staticBarrier.children;
        for (let i = 0, c = children.length; i < c; ++i) {
            let node = children[i];
            let n = node.name;
            n = n.split("-")[0];
            let e = node.eulerAngles.y;
            let s = node.scale.x;
            let p = node.getWorldPosition();
            points.push({
                n: n,
                p: { x: parseFloat(p.x.toFixed(2)), y: 0, z: parseFloat(p.z.toFixed(2)) },
                e: e,
                s: s,
            });
        }
        points.sort((a, b) => {
            return a.n.localeCompare(b.n);
        });
        return points;
    }
    //#endregion

    //#region 可撞开障碍物
    @property(Node)
    protected dynamicBarrier: Node = null;
    protected getDynamicBarriers() {
        let points = [];
        let children = this.dynamicBarrier.children;
        for (let i = 0, c = children.length; i < c; ++i) {
            let node = children[i];
            let n = node.name;
            n = n.split("-")[0];
            let e = node.eulerAngles.y;
            let s = node.scale.x;
            let p = node.getWorldPosition();
            points.push({
                n: n,
                p: { x: parseFloat(p.x.toFixed(2)), y: 0, z: parseFloat(p.z.toFixed(2)) },
                e: e,
                s: s,
            });
        }
        points.sort((a, b) => {
            return a.n.localeCompare(b.n);
        });
        return points;
    }
    //#endregion

    //#region 可破坏障碍物
    @property(Node)
    protected breakBarrier: Node = null;
    protected getBreakBarrier() {
        let points = [];
        let children = this.breakBarrier.children;
        for (let i = 0, c = children.length; i < c; ++i) {
            let node = children[i];
            let n = node.name;
            n = n.split("-")[0];
            let e = node.eulerAngles.y;
            let s = node.scale.x;
            let p = node.getWorldPosition();
            points.push({
                n: n,
                p: { x: parseFloat(p.x.toFixed(2)), y: 0, z: parseFloat(p.z.toFixed(2)) },
                e: e,
                s: s,
            });
        }
        points.sort((a, b) => {
            return a.n.localeCompare(b.n);
        });
        return points;
    }

    //#endregion

    getData() {
        let data: any = {};
        data.playerPos = this.getPlayerPos();
        data.portalPos = this.getPortalPos();
        data.enemyPos = this.getEnemiesPos();
        data.collider = this.getCollider();
        data.staticBarriers = this.getStaticBarriers();
        data.dynamicBarriers = this.getDynamicBarriers();
        data.breakBarriers = this.getBreakBarrier();
        let xMin = data.collider[0][0];
        let xMax = data.collider[0][0];
        let zMin = data.collider[0][1];
        let zMax = data.collider[0][1];
        for (let i = data.collider.length - 1; i >= 0; --i) {
            for (let j = 0; j < data.collider[i].length; j += 2) {
                let x = data.collider[i][j];
                let y = data.collider[i][j + 1];
                if (xMin > x) {
                    xMin = x;
                }
                if (xMax < x) {
                    xMax = x;
                }
                if (zMin > -y) {
                    zMin = -y;
                }
                if (zMax < -y) {
                    zMax = -y;
                }
            }
        }
        data.scope = {
            xMin: xMin,
            xMax: xMax,
            zMin: zMin,
            zMax: zMax,
        };
        return data;
    }
    start() {
        console.log("碰撞体数据：");
        let data = this.getData();
        console.log(JSON.stringify(data));

    }

}